package cn.tyf.dao;


import cn.tyf.domain.Record;
import cn.tyf.domain.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.List;

@Repository
public interface RecordDao {
    /**
     * 查询总记录数
     * @return
     */
    @Select("select COUNT(*)  from zvisit;")
    Integer findByCount();

    @Select("select COUNT(*)  from zvisit left JOIN user ON user.rid = zvisit.rid where name like '%${name}%'")
    Integer findByNameCount(@Param("name")String name);

    /**
     * 查询总访问记录,按照最新排序
     * @param start
     * @param pageSize
     * @return
     */
    @Select("select user.`name`,zvisit.*  from zvisit left JOIN user ON user.rid = zvisit.rid  WHERE " +
            " user.`name` like '%${arg0}%' order by zvisit.time DESC limit #{arg1},#{arg2} ")
    List<Record> findByNamePage(String name,Integer start, Integer pageSize);

    @Select("select user.`name`,zvisit.*  from zvisit left JOIN user ON user.rid = zvisit.rid  WHERE 1 = 1 " +
            "        order by zvisit.time DESC limit #{arg0},#{arg1}")
    List<Record> findByPage(Integer start, Integer pageSize);


    /**
     * 查询用户访问总记录数
     * @param rid
     * @return
     */
   /* @Select("select  count(*)  from zvisit left JOIN user ON user.rid = zvisit.rid WHERE  zvisit.rid = #{rid}")
   */ Integer findByUserCount(String rid,String time);

    /**
     * 分页查询用户记录
     * @param rid
     * @param time
     * @param start
     * @param pageSize
     * @return
     */
   /* @Select("select  user.name,zvisit.*  from zvisit left JOIN user ON user.rid = zvisit.rid " +
            "  WHERE  zvisit.rid = #{arg0} limit #{arg1},#{arg2} ")*/
    List<Record> findPageByUser(String rid,String time, Integer start, Integer pageSize);

    @Insert("insert into zvisit(rid,time,door) value(#{arg0},#{arg1},#{arg2})")
    void addAnnal(String card, String time, Integer door);

    @Select("select user.`name`,zvisit.*  from zvisit left JOIN user ON user.rid = zvisit.rid")
    List<Record> findAll();
}
